#This file serves as a location for placing code that is old, unused, incorrect, obsolete, etc. 

#Using the current model, run through the test dataset to see how accurate it is.
def scoreModel(wordBag, totals):
	testingFileStream = FileIO.openTestingFile(FileIO.READ_MODE)
	Parser.readHeader(testingFileStream)
	
	totalPosts = 0
	correctlyIdentifiedPosts = 0
	correctlyIdentifiedOpen = 0
	correctlyIdentifiedOffTopic = 0
	correctlyIdentifiedNotConstructive = 0
	correctlyIdentifiedNotARealQuestion = 0
	correctlyIdentifiedTooLocalized = 0
	
	while (not Parser.isEndOfFile(testingFileStream)):
		testPost = Parser.readPost(testingFileStream)
		actualStatus =  testPost.openStatus
		totalPosts = totalPosts + 1
		
		#Get the numeric preditions for each status
		
		openPrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.OPEN)
		offTopicPrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.OFF_TOPIC)
		notConstructivePrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.NOT_CONSTRUCTIVE)
		notARealQuestionPrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.NOT_A_REAL_QUESTION)
		tooLocalizedPrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.TOO_LOCALIZED)
		predictions = [openPrediction, offTopicPrediction, notConstructivePrediction, notARealQuestionPrediction, tooLocalizedPrediction]
		
		#The status with the max score is the actual prediction.
		#If the prediction is correct, increment the correct variable.
		if(actualStatus == Post.PostStatusEnum.OPEN):
			if(max(predictions)==openPrediction):
				correctlyIdentifiedPosts += 1
				correctlyIdentifiedOpen += 1
		if(actualStatus == Post.PostStatusEnum.OFF_TOPIC):
			if(max(predictions)==offTopicPrediction):
				correctlyIdentifiedPosts += 1
				correctlyIdentifiedOffTopic += 1
		if(actualStatus == Post.PostStatusEnum.NOT_CONSTRUCTIVE):
			if(max(predictions)==notConstructivePrediction):
				correctlyIdentifiedPosts += 1
				correctlyIdentifiedNotConstructive += 1
		if(actualStatus == Post.PostStatusEnum.NOT_A_REAL_QUESTION):
			if(max(predictions)==notARealQuestionPrediction):
				correctlyIdentifiedPosts += 1
				correctlyIdentifiedNotARealQuestion +=1
		if(actualStatus == Post.PostStatusEnum.TOO_LOCALIZED):
			if(max(predictions)==tooLocalizedPrediction):
				correctlyIdentifiedPosts += 1
				correctlyIdentifiedTooLocalized +=1
	
	accuracy = float(correctlyIdentifiedPosts) / float(totalPosts)
	print str(accuracy)
	print str(correctlyIdentifiedOpen) +" "+ str(correctlyIdentifiedOffTopic) +" "+ str(correctlyIdentifiedNotConstructive) +" "+ str(correctlyIdentifiedNotARealQuestion) +" "+ str(correctlyIdentifiedTooLocalized)

def scoreModelLogLoss(wordBag, totals):
	testingFileStream = FileIO.openTestingFile(FileIO.READ_MODE)
	Parser.readHeader(testingFileStream)
	
	totalPosts = 0
	logTotal = 0
	
	while (not Parser.isEndOfFile(testingFileStream)):
		testPost = Parser.readPost(testingFileStream)
		actualStatus =  testPost.openStatus
		totalPosts = totalPosts + 1
		
		#Get the numeric preditions for each status
		
		openPrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.OPEN)
		offTopicPrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.OFF_TOPIC)
		notConstructivePrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.NOT_CONSTRUCTIVE)
		notARealQuestionPrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.NOT_A_REAL_QUESTION)
		tooLocalizedPrediction = predictPost(testPost, wordBag, totals, Post.PostStatusEnum.TOO_LOCALIZED)
		totalPrediction = openPrediction + offTopicPrediction + notConstructivePrediction + notARealQuestionPrediction + tooLocalizedPrediction
		
		normalizedOpenPrediction = openPrediction / totalPrediction
		normalizedOffTopicPrediction = offTopicPrediction / totalPrediction
		normalizedNotConstructivePrediction = notConstructivePrediction / totalPrediction
		normalizedNotARealQuestionPrediction = notARealQuestionPrediction / totalPrediction
		normalizedTooLocalizedPrediction = tooLocalizedPrediction / totalPrediction
		
		#print str(normalizedOpenPrediction) + " " + str(normalizedOffTopicPrediction) + " " + str(normalizedNotConstructivePrediction) + " " + str(normalizedNotARealQuestionPrediction) + " " + str(normalizedTooLocalizedPrediction)
		
		#The status with the max score is the actual prediction.
		#If the prediction is correct, increment the correct variable.
		if(actualStatus == Post.PostStatusEnum.OPEN):
			logTotal += math.log(normalizedOpenPrediction)
		if(actualStatus == Post.PostStatusEnum.OFF_TOPIC):
			logTotal += math.log(normalizedOffTopicPrediction)
		if(actualStatus == Post.PostStatusEnum.NOT_CONSTRUCTIVE):
			logTotal += math.log(normalizedNotConstructivePrediction)
		if(actualStatus == Post.PostStatusEnum.NOT_A_REAL_QUESTION):
			logTotal += math.log(normalizedNotARealQuestionPrediction)
		if(actualStatus == Post.PostStatusEnum.TOO_LOCALIZED):
			logTotal += math.log(normalizedTooLocalizedPrediction)
	
	logLoss = float(-1)*((float(1)/float(totalPosts))* (logTotal))
	print logLoss

def predictPost(post,wordBag,postStatus,statusBaseLine):
	prob = float(1)
	invProb = float(1)
	words = splitAndCleanWords(post.bodyMarkdown)
	for word in words:
		if(wordBag.contains(word)):
			numOccurences = wordBag.get(word).totalCount()
			if(numOccurences >= 3): #TODO Improve
				thisProb =wordBag.get(word).getProbabilityOfStatus(postStatus,statusBaseLine)
				if(thisProb<1): #TODO Find solution
					prob = prob * thisProb
					invProb = invProb * (1 -thisProb)
	print "Prob: " + str(prob) + "  InvProb: " + str(invProb)
	return prob / (prob + invProb)

openBaseline = float(openCount) / float(postCount)
offTopicBaseline = float(offTopicCount) / float(postCount)
notConstructiveBaseline = float(notConstructiveCount) / float(postCount)
notARealQuestionBaseline = float(notARealQuestionCount) / float(postCount)
tooLocalizedBaseline = float(tooLocalizedCount) / float(postCount)

def getProbabilityOfStatus(self, postStatus, baseline):
	statusCount = float(self.getCountByStatus(postStatus))
	totalCount = float(self.totalCount())
	overallProbability = statusCount / totalCount
	adjustedProbability = float(.5)
	if(overallProbability > baseline):
		maxDiff = float(1) - baseline
		diff = overallProbability - baseline
		adjustedProbability = float(.5) + diff/maxDiff
	else:
		maxDiff = overallProbability
		diff = baseline - overallProbability
		if(maxDiff == 0):
			adjustedProbability = float(1)
		else:
			adjustedProbability = float(.5) - diff/maxDiff
	return adjustedProbability
	
def scoreModel(wordBag, totals):
	testingFileStream = FileIO.openTestingFile(FileIO.READ_MODE)
	
	totalOpenAccuracy = 0
	openPosts = 0
	totalOffTopicAccuracy = 0
	offTopicPosts = 0
	totalNotConstructiveAccuracy = 0
	notConstructivePosts = 0
	totalNotARealQuestionAccuracy = 0
	notARealQuestionPosts = 0
	totalTooLocalizedAccuracy = 0
	tooLocalizedPosts = 0
	totalPosts = 0
	while (not Parser.isEndOfFile(testingFileStream)):
		testPost = Parser.readPost(testingFileStream)
		actualStatus =  testPost.openStatus
		totalPosts = totalPosts + 1
		print "Open Prediction " + str(predictPost2(testPost, wordBag, totals, Post.PostStatusEnum.OPEN))
		if(actualStatus == Post.PostStatusEnum.OPEN):
			openPosts = openPosts + 1
			totalOpenAccuracy = totalOpenAccuracy + predictPost2(testPost, wordBag, totals, Post.PostStatusEnum.OPEN)
		if(actualStatus == Post.PostStatusEnum.OFF_TOPIC):
			offTopicPosts = offTopicPosts + 1
			totalOffTopicAccuracy = totalOffTopicAccuracy + predictPost2(testPost, wordBag, totals, Post.PostStatusEnum.OFF_TOPIC)
		if(actualStatus == Post.PostStatusEnum.NOT_CONSTRUCTIVE):
			notConstructivePosts = notConstructivePosts + 1
			totalNotConstructiveAccuracy = totalNotConstructiveAccuracy + predictPost2(testPost, wordBag, totals, Post.PostStatusEnum.NOT_CONSTRUCTIVE)
		if(actualStatus == Post.PostStatusEnum.NOT_A_REAL_QUESTION):
			notARealQuestionPosts = notARealQuestionPosts + 1
			totalNotARealQuestionAccuracy = totalNotARealQuestionAccuracy + predictPost2(testPost, wordBag, totals, Post.PostStatusEnum.NOT_A_REAL_QUESTION)
		if(actualStatus == Post.PostStatusEnum.TOO_LOCALIZED):
			tooLocalizedPosts = tooLocalizedPosts + 1
			totalTooLocalizedAccuracy = totalTooLocalizedAccuracy + predictPost2(testPost, wordBag, totals, Post.PostStatusEnum.TOO_LOCALIZED)
	print "Average Open Accuracy:             " + str(totalOpenAccuracy/float(openPosts))
	print "Average OffTopic Accuracy:         " + str(totalOffTopicAccuracy/float(offTopicPosts))
	print "Average NotConstructive Accuracy:  " + str(totalNotConstructiveAccuracy/float(notConstructivePosts))
	print "Average NotARealQuestion Accuracy: " + str(totalNotARealQuestionAccuracy/float(notARealQuestionPosts))
	print "Average TooLocalized Accuracy:     " + str(totalTooLocalizedAccuracy/float(tooLocalizedPosts))
	
def predictPost(post, wordBag, totals, statusToPredict):
	classProbability = float(.2)#totals.getPostStatusRatio(statusToPredict)
	wordProbabilityForClass = float(1)
	wordProbabilityOverall = float(1)
	words = WordProcessor.processText(post.bodyMarkdown)
	for word in words:
		if(wordBag.contains(word)):
			wordProbabilityForClass = wordProbabilityForClass * wordBag.get(word).getProbabilityOfStatus(statusToPredict)
			wordProbabilityOverall = wordProbabilityOverall * (float(wordBag.get(word).totalCount())/float(totals.getTotal()))
			if(wordProbabilityOverall == 0):
				print (float(wordBag.get(word).totalCount())/float(totals.getTotal()))
				print float(wordBag.get(word).totalCount())
				print float(totals.getTotal())
				sys.exit("abc")
			print "Word" + word
			print "This Words Probability For Class " + str(wordBag.get(word).getProbabilityOfStatus(statusToPredict))
			print "This Words Probability Overall " + str(float(wordBag.get(word).totalCount())/float(totals.getTotal()))
	print "Class Probability " + str(classProbability)
	print "Word Probability For Class " + str(wordProbabilityForClass)
	print "Word Probability Overall " + str(wordProbabilityOverall)
	return (wordProbabilityForClass * classProbability) / wordProbabilityOverall

#def predictPost3(post, wordBag, totals, statusToPredict):
	#classProbability = totals.getPostStatusRatio(statusToPredict)
	#totalProbability = float(1)
	#words = wordprocessor.processText(post.bodyMarkdown)
	#for word in wordBag:
	#	if(wordBag.contains(word)):
	#		wordProbabilityNum = float(1) + 
	#		wordProbabilityDenom